package com.edu.controller.impl;

import com.edu.annotation.EnableRedisCache;
import com.edu.controller.StudentController;
import com.edu.pojo.*;
import com.edu.service.StudentService;
import com.edu.util.RedisClientUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
//import org.apache.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @作者 five-five
 * @创建时间 2021/1/11
 */
@Api(tags = "学生管理相关接口")
@RestController
//@RequestMapping("/stu")
public class StudentControllerImpl implements StudentController {
    //集成日志logger对象 这是log4j的
//    Logger logger = Logger.getLogger(StudentControllerImpl.class);
    //这是log4j2的
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Autowired
    private StudentService studentService;
    @Autowired
    private RedisClientUtil redisClientUtil;

    public String index() {
        return "index";
    }

    /**
     * 跳转至首页，测试下视图解析器(发现不配做视图解析器，也能跑)
     *
     * @return
     */
    @ApiOperation(notes = "跳转至首页，测试下视图解析器", value = "跳转至首页，测试下视图解析器")
    public APIResult<PageInfo<Student>> front(
            @ApiParam(name = "apiRequest", value = "apiRequest")
            @RequestBody APIRequest apiRequest
    ) {
        logger.info("---------------------这是一条日志----------------------");
        APIResult<PageInfo<Student>> result = new APIResult<>();
        List<Student> all = studentService.getAll(apiRequest);
        PageInfo<Student> studentPageInfo = new PageInfo<>(all);
        result.setData(studentPageInfo);
        result.setState(StateEnum.SUCCESS.getCode());
        result.setMessage(StateEnum.SUCCESS.getText());
        return result;
    }

    /**
     * map可以不要
     *
     * @return
     */
    @EnableRedisCache(value = "stus")
    @ApiOperation("展示所有学生")
    public Object list(
            @ApiParam(name = "apiRequest", value = "apiRequest")
            @RequestBody APIRequest apiRequest
    ) {
        Map<String, Object> allToMap = studentService.getAllToMap(apiRequest);
        return allToMap;
    }

    /**
     * 根据学生id进行删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据学生id进行删除", notes = "根据学生id进行删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生id", defaultValue = "0")
    })
    public Object delbyId(@RequestParam(required = true) Integer id) {
        APIResult<Void> apiResult = new APIResult<>();
        int i = studentService.delById(id);
        if (i != -1) {
            apiResult.setMessage("删除成功");
            redisClientUtil.del("stus");
        } else {
            apiResult.setMessage("删除失败");
        }
        return apiResult;
//        return "Redirect";
//        return "Forward";
    }

    /**
     * 根据学生id进行删除
     *
     * @param num
     * @return
     */
    @ApiOperation(notes = "根据num批量添加学生", value = "根据num批量添加学生")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "num", value = "学生个数", defaultValue = "1")
//    })
    public Object batcheAddStus(
            @ApiParam(name = "num", value = "增加学生个数", required = true)
            @RequestParam(required = true) Integer num) {
        APIResult<Void> apiResult = new APIResult<>();
        studentService.batcheAddStus(num);
        return apiResult;
//        return "Redirect";
//        return "Forward";
    }

    @ApiOperation(notes = "上传文件至ftp文件服务器", value = "上传文件至ftp文件服务器")
    public APIResult<Boolean> uploadFile(
            @ApiParam(name = "file", value = "传入的文件", required = true)
            @RequestBody(required = true) MultipartFile file) {
        APIResult<Boolean> result = new APIResult<>();
        studentService.uploadFile(file);
        result.setMessage("文件上传成功");
        return result;
    }
}
